//
//  MPTControlView.m
//  MPTLive
//
//  Created by caixun on 15/8/20.
//  Copyright (c) 2015年 Jeakin. All rights reserved.
//

#import "MPTControlView.h"
#import "MZTimerLabel.h"
#import "UIFont+MPTFont.h"


#pragma mark - ******************************** 公共宏 **********************************
// 距屏幕左侧间距
#define Left_Space (10)
// 距屏幕上部间距
#define Top_Space (20)
// live图片和倒计时lab的间距
#define LiveImage_LabTime_Space (5)
// 距离屏幕右侧间距
#define Right_Space (10)

#pragma mark - ******************************** labTime相关宏 **********************************
// labTime宽度
#define LabTimeW (150)
// labTime字体大小
#define LabTimeFontSize (12.0f)

#pragma mark - ******************************** 功能按钮间距宏 **********************************
// 关闭按钮和摄像按钮的间距
#define CloseBtn_ReversalBtn_Space (22)
// 闪光灯和摄像按钮的间距
#define FlashlightBtn_ReversalBtn_Space (15)


@interface MPTControlView ()

// 记录播放时间
@property (nonatomic, strong) MZTimerLabel *labTime;

// 控制台按钮
@property (nonatomic, strong) UIButton *butKongZhi;

// 直播图片
@property (nonatomic, strong) UIImageView *liveImageView;

/// 点点
@property (nonatomic, strong) UIImageView *imageVDianDian;

@property (nonatomic, strong) NSTimer *timer;

/// 人数赞数
@property (nonatomic, strong) TPLabel *labRenShuZanShu;

@end

@implementation MPTControlView

#pragma mark - ***************************** View Lifecycle ***********************************

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initMainViews];
    }
    return self;
}

-(void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];
    
    if(newSuperview == nil)
    {
        [[self subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        return ;
    }
}

- (void)dealloc
{
    [_timer fire];
    _timer = nil;
}

#pragma mark - ******************************** Init All Views **********************************

- (void)initMainViews
{
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, 0, screenW, self.frame.size.height);
    }];
    
    UIImage *controlImg = [UIImage imageNamed:@"ControlViewBg"];
    UIImageView *controlBg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenW, controlImg.size.height)];
    controlBg.image = controlImg;
    [self addSubview:controlBg];
    
    // view左上部分的live图标
    UIImage *liveImg = [UIImage imageNamed:@"zhiboLive"];
    CGRect liveImage = CGRectMake(Left_Space, Top_Space, liveImg.size.width, liveImg.size.height);
    self.liveImageView = [[UIImageView alloc] initWithFrame:liveImage];
    [self.liveImageView setImage:liveImg];
    [self addSubview:self.liveImageView];
    
    // 记录播放时间
    CGRect labTimeRect = CGRectMake(self.liveImageView.frame.origin.x+self.liveImageView.frame.size.width+LiveImage_LabTime_Space,
                                    Top_Space - 3,
                                    LabTimeW,
                                    25);
    self.labTime = [[MZTimerLabel alloc] initWithFrame:labTimeRect];
    self.labTime.timerType = MZTimerLabelTypeStopWatch;
    [self addSubview:self.labTime];
    self.labTime.timeLabel.backgroundColor = [UIColor clearColor];
    self.labTime.timeLabel.font = [UIFont YXFontOfSize:LabTimeFontSize];
    self.labTime.timeLabel.textColor = [UIColor whiteColor];
    self.labTime.shadowColor = [UIColor colorWithWhite:0.1f alpha:0.8f];
    self.labTime.shadowOffset = CGSizeMake(0.5f, 0.5f);
    self.labTime.timeLabel.textAlignment = NSTextAlignmentLeft;
    
    /// 点点
    UIImage *iamgeDian = [UIImage imageNamed:@"luzhiDianDian"];
    self.imageVDianDian = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.labTime.frame) / 2 + 5, CGRectGetMaxY(self.labTime.frame) / 2 + 4 , iamgeDian.size.width, iamgeDian.size.height)];
    self.imageVDianDian.image = iamgeDian;
    self.imageVDianDian.hidden = YES;
    [self addSubview:_imageVDianDian];
    
    // 关闭按钮
    UIImage *closeImg = [UIImage imageNamed:@"kongzhitai"];
    UIImage *closeImgPress = [UIImage imageNamed:@"kongzhitaiS"];
    CGRect closeRect = CGRectMake(screenW-Right_Space-closeImg.size.width,
                                  Top_Space,
                                  closeImg.size.width,
                                  closeImg.size.height);
    self.butKongZhi = [UIButton buttonWithType:UIButtonTypeCustom];
    self.butKongZhi.frame = closeRect;
    [self.butKongZhi setImage:closeImgPress forState:UIControlStateHighlighted];
    [self.butKongZhi setImage:closeImg forState:UIControlStateNormal];
    [self addSubview:self.butKongZhi];
    
    /// 人数赞数
    _labRenShuZanShu = [TPLabel getWithFrame:CGRectMake(CGRectGetMaxX(_liveImageView.frame) + LiveImage_LabTime_Space, CGRectGetMaxY(_labTime.frame) - 5, screenW - 130, 20) text:@"" textColor:[UIColor whiteColor] textFont:[UIFont systemFontOfSize:15]];
    _labRenShuZanShu.text = @"当前关注1000/ 被赞 232423453";
    [self addSubview:_labRenShuZanShu];
}


#pragma mark - ****************************** Functions *********************************

- (void)setTargetForBtn:(id)target  withKongZhiTai:(SEL)action
{
    [self.butKongZhi addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
}


- (void)beginTime
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(updateDianDian) userInfo:nil repeats:YES];
    [self.labTime start];
}

- (void)pauseTime
{
    [self.labTime pause];
}

- (void)updateDianDian
{
    _imageVDianDian.hidden = !_imageVDianDian.hidden;
}

- (void)changeLiveImage:(UIImage *)livingImg
{
    [self.liveImageView setImage:livingImg];
}


@end
